package com.cookor.poi.modules.phy.mobile;

import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.ResultUtil;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.modules.phy.dto.TSchedueMpDto;
import com.cookor.poi.modules.phy.entity.TBookNo;
import com.cookor.poi.modules.phy.entity.TDoctor;
import com.cookor.poi.modules.phy.entity.TEvaluate;
import com.cookor.poi.modules.phy.entity.TFork;
import com.cookor.poi.modules.phy.entity.TSchedue;
import com.cookor.poi.modules.phy.service.TBookNoService;
import com.cookor.poi.modules.phy.service.TDoctorService;
import com.cookor.poi.modules.phy.service.TEvaluateService;
import com.cookor.poi.modules.phy.service.TForkService;
import com.cookor.poi.modules.phy.service.TSchedueService;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"/api/phy/doctor"})
@ResponseBody
public class ApiDoctorController extends ApiBaseController {
  @Autowired
  private TSchedueService tSchedueService;
  
  @Autowired
  private TBookNoService bookNoService;
  
  @Autowired
  private TDoctorService tDoctorService;
  
  @Autowired
  private TForkService tForkService;
  
  @Autowired
  private TEvaluateService tEvaluateService;
  
  @RequestMapping({"/data"})
  public ResultUtil data(HttpServletRequest request, Integer doctorId, String openId) {
    ResultUtil result = getResult();
    try {
      Integer tenantId = getTenantId(request);
      if (doctorId == null || StringUtils.isBlank(openId))
        throw new Exception("参数错误"); 
      TDoctor tDoctor = this.tDoctorService.get(doctorId.toString());
      Calendar c = Calendar.getInstance();
      int month = c.get(2) + 1;
      int year = c.get(1);
      TSchedue now = this.tSchedueService.findByDoctorId(tenantId, doctorId, Integer.valueOf(year), Integer.valueOf(month));
      c.add(2, 1);
      month = c.get(2) + 1;
      year = c.get(1);
      TSchedue next = this.tSchedueService.findByDoctorId(tenantId, doctorId, Integer.valueOf(year), Integer.valueOf(month));
      List<TSchedueMpDto> list = this.tSchedueService.getMpDate(tDoctor, now, next);
      TFork fork = this.tForkService.findByParam(openId, doctorId);
      result.putParams("fork", Integer.valueOf((fork == null) ? 0 : fork.getStatus().intValue()));
      result.putParams("doctor", tDoctor);
      result.putParams("address", tDoctor.getTenant().getTenantName());
      result.putParams("schedue", list);
      TEvaluate tEvaluate = new TEvaluate();
      tEvaluate.setDelFlag("0");
      tEvaluate.setDoctorId(Integer.valueOf(tDoctor.getId()));
      tEvaluate.setTenantId(tenantId);
      List<TEvaluate> evaList = this.tEvaluateService.findList(tEvaluate);
      int good = 0;
      int bad = 0;
      for (TEvaluate eva : evaList) {
        if (eva.getStar().intValue() >= 3) {
          good++;
          continue;
        } 
        bad++;
      } 
      result.putParams("evaStatus", (bad == 0) ? Integer.valueOf(100) : (new BigDecimal(good)).divide(new BigDecimal(good + bad))
          .setScale(2, RoundingMode.UP).multiply(new BigDecimal(100)));
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("ApiDoctorController typeList error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/bookNo"})
  public ResultUtil bookNo(HttpServletRequest request, Integer doctorId, Integer year, Integer month, Integer day, Integer type) {
    ResultUtil result = getResult();
    try {
      Integer tenantId = getTenantId(request);
      List<TBookNo> bookNoList = this.bookNoService.queryByAppoint(tenantId, null, doctorId, year, month, day, type, Integer.valueOf(0));
      result.putParams("list", bookNoList);
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("ApiDoctorController data error!", e);
    } 
    return result;
  }
  
  @RequestMapping({"/list"})
  public ResultUtil doctorList(HttpServletRequest request, Integer officeId, Integer tenantId) {
    ResultUtil result = getResult();
    try {
      TDoctor doctor = new TDoctor();
      doctor.setTenantId(tenantId);
      doctor.setOfficeId(officeId);
      doctor.setDelFlag("0");
      Page<TDoctor> page = this.tDoctorService.findPage(new Page(1, -1), doctor);
      result.putParams("page", page);
    } catch (Exception e) {
      result.setCode(ResultUtil.ERROR_CODE);
      result.setError(e.getMessage());
      this.logger.error("ApiDoctorController doctorList error!", e);
    } 
    return result;
  }
}
